Adding a bunch of hooks from wikiHow into DifferenceEngine, 2nd try
[lhc/web/wiklou.git] / includes / libs / objectcache / RESTBagOStuff.php
1 <?php
2
3 /**
4 * Interface to key-value storage on HTTP RESTful server, such as RESTBase.
5 * Uses URL of the form URL/{KEY} to store/fetch/delete.
6 * E.g., when base URL is /v1/sessions/ then the store would do:
7 * PUT /v1/sessions/12345758
8 * and fetch would do:
9 * GET /v1/sessions/12345758
10 * delete would do:
11 * DELETE /v1/sessions/12345758
12 *
13 * Configure with:
14 * @code
15 * $wgObjectCaches['sessions'] = array(
16 * 'class' => 'RESTBagOStuff',
17 * 'url' => 'http://localhost:7231/wikimedia.org/v1/sessions/'
18 * );
19 * @endcode
20 */
21 class RESTBagOStuff extends BagOStuff {
22
23 /**
24 * @var MultiHttpClient
25 */
26 private $client;
27
28 /**
29 * REST URL to use for storage.
30 * @var string
31 */
32 private $url;
33
34 public function __construct( $params ) {
35 if ( empty( $params['url'] ) ) {
36 throw new InvalidArgumentException( 'URL parameter is required' );
37 }
38 parent::__construct( $params );
39 if ( empty( $params['client'] ) ) {
40 $this->client = new MultiHttpClient( [] );
41 } else {
42 $this->client = $params['client'];
43 }
44 // Make sure URL ends with /
45 $this->url = rtrim( $params['url'], '/' ) . '/';
46 }
47
48 /**
49 * @param string $key
50 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
51 * @return mixed Returns false on failure and if the item does not exist
52 */
53 protected function doGet( $key, $flags = 0 ) {
54 $req = [
55 'method' => 'GET',
56 'url' => $this->url . rawurlencode( $key ),
57
58 ];
59 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
60 if ( $rcode === 200 ) {
61 if ( is_string( $rbody ) ) {
62 return unserialize( $rbody );
63 }
64 return false;
65 }
66 if ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
67 return $this->handleError( "Failed to fetch $key", $rcode, $rerr );
68 }
69 return false;
70 }
71
72 /**
73 * Handle storage error
74 * @param string $msg Error message
75 * @param int $rcode Error code from client
76 * @param string $rerr Error message from client
77 * @return false
78 */
79 protected function handleError( $msg, $rcode, $rerr ) {
80 $this->logger->error( "$msg : ({code}) {error}", [
81 'code' => $rcode,
82 'error' => $rerr
83 ] );
84 $this->setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );
85 return false;
86 }
87
88 /**
89 * Set an item
90 *
91 * @param string $key
92 * @param mixed $value
93 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
94 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
95 * @return bool Success
96 */
97 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
98 $req = [
99 'method' => 'PUT',
100 'url' => $this->url . rawurlencode( $key ),
101 'body' => serialize( $value )
102 ];
103 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
104 if ( $rcode === 200 || $rcode === 201 ) {
105 return true;
106 }
107 return $this->handleError( "Failed to store $key", $rcode, $rerr );
108 }
109
110 /**
111 * Delete an item.
112 *
113 * @param string $key
114 * @return bool True if the item was deleted or not found, false on failure
115 */
116 public function delete( $key ) {
117 $req = [
118 'method' => 'DELETE',
119 'url' => $this->url . rawurlencode( $key ),
120 ];
121 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
122 if ( $rcode === 200 || $rcode === 204 || $rcode === 205 ) {
123 return true;
124 }
125 return $this->handleError( "Failed to delete $key", $rcode, $rerr );
126 }
127 }